import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/home/Home')
  }, {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/Login'),
    beforeEnter (to, from, next) {
      const { login } = localStorage
      login ? next({ name: 'Home' }) : next()
    }
  }, {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register/Register')
  }, {
    path: '/shop/:id',
    name: 'Shop',
    component: () => import('../views/shop/Shop')
  }, {
    path: '/cary',
    name: 'Cart',
    component: () => import('../views/cartList/Cart')
  }, {
    path: '/order-confirmation/:shopId',
    name: 'OrderConfirmation',
    component: () => import('../views/orderConfirmation/OrderConfirmation')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
const whiterList = ['Login', 'Register']
router.beforeEach((to, from, next) => {
  const { isLogin } = localStorage
  const { name } = to;
  (isLogin || whiterList.includes(name)) ? next() : next({ name: 'Login' })
})

export default router
